import * as api from "@/api/permission"
import allRoutes from "@/router/generateRoute"
// 把函数递归创建用户路由的方法引入
import recursionRoutes from "@/router/recursionRoutes";
// 引入路由
import router from "@/utils/routerGuard";

// 引入动态路由
import dynamicRoute from "@/router/dynamicRoutes";
let routes = allRoutes.find(route => route.name === "home")


export default {
    namespaced: true,
    state: {
        userMenu: []
    },
    getters: {},
    mutations: {
        // 设置用户菜单
        SET_USER_MENU(state, payload) {
            state.userMenu = [...payload]
            dynamicRoute.children=[...payload]
            router.addRoute(dynamicRoute)
        }
    },
    actions: {
        // 获取远程菜单
        FETCH_MENU_LIST({ commit }) {
            return api.getMenuListApi()
                .then(res => {
                    console.log(res.data.menuList)
                    let userMenu = recursionRoutes(res.data.menuList, routes.children)
                    commit("SET_USER_MENU", userMenu)
                })
        }
    },
    modules: {},
};